package com.framework2.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CalendarView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.framework.utils.ScreenUtils;
import com.framework.utils.Y;
import com.framework2.baseEvent.BaseOnClickListener;
import com.yaxin.yyt.R;

import java.text.DecimalFormat;

/**
 * 选择日期
 *
 * @author YobertJomi
 * className ChooseDatePopupwindow
 * created at  2017/5/17  14:58
 */

public class ChooseDatePopupwindow extends PopupWindow {
    private Context context;
    private View viewRoot;
    private CalendarView calendarView;
    private OnSelectedListener onSelectedListener;

    public ChooseDatePopupwindow(Context context) {
        super(context);
        this.context = context;
        initView();
        initStyle();
        initEvent();
    }

    private void initEvent() {
        if (calendarView != null) {
            calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                @Override
                public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                    Y.y("" + year + "  " + month + "  " + dayOfMonth);
                    if (null != onSelectedListener) {
                        DecimalFormat decimalFormat = new DecimalFormat();
                        decimalFormat.applyPattern("00");
                        onSelectedListener.onSelected(year + "-" + decimalFormat.format(month + 1) + "-" +
                                decimalFormat.format(dayOfMonth));
                    }
                }
            });
        }
        if (null != viewRoot) {
            viewRoot.setOnClickListener(new BaseOnClickListener() {
                @Override
                protected void onBaseClick(View v) {
                    dismiss();
                }
            });
        }
    }

    private void initView() {
        viewRoot = LayoutInflater.from(context).inflate(
                R.layout.popwindow_choose_date, null);
        calendarView = (CalendarView) viewRoot.findViewById(R.id.calendarView);

        setWidth(ScreenUtils.getInstance().getScreenWidthPx(context));
        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
//        int height=(int)((ScreenUtils.getInstance().getScreenHeightPx(context)-ScreenUtils.getInstance()
// .getStatusBarHeightPx(context)-ScreenUtils.getInstance().dip2px(context,50))*0.34);
//        PercentRelativeLayout.LayoutParams layoutParams=new PercentRelativeLayout.LayoutParams(ScreenUtils
// .getInstance().getScreenWidthPx(context)-ScreenUtils.getInstance().dip2px(context,40),height);
//        viewRoot.findViewById(R.id.rootView).setLayoutParams(layoutParams);
        setContentView(viewRoot);
    }

    private void initStyle() {
        // 设置整个popupwindow的弹出，收起方式
        setAnimationStyle(R.style.popWindowAnim);
        // 需要设置一下此参数，点击外边可消失
        setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        setFocusable(true);
    }

    public ChooseDatePopupwindow setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
        return this;
    }

    public ChooseDatePopupwindow setDate(long date) {
        calendarView.setDate(date);
        calendarView.setSelected(true);
        return this;
    }

    public interface OnSelectedListener {
        void onSelected(String info);
    }
}
